package com.hp.it.portal.handler;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

public class SonarReportConfigurationHandler
{

	public boolean handle(String action, HttpServletRequest req)
	{
		boolean result = false;
		if ("U".equalsIgnoreCase(action))
		{
			result = update(req);
		} else if ("C".equalsIgnoreCase(action))
		{
			result = create(req);
		}
		else if("D".equalsIgnoreCase(action))
		{
			result = delete(req);
		}
		return result;
	}

	private boolean update(HttpServletRequest req)
	{
		Properties properties = new Properties();
		properties.setProperty("KEE", req.getParameter("KEE"));
		properties.setProperty("PORTAL_URL", req.getParameter("PORTAL_URL"));
		properties.setProperty("DB_DRIVER", req.getParameter("DB_DRIVER"));
		properties.setProperty("DB_URL", req.getParameter("DB_URL"));
		properties.setProperty("DB_USER", req.getParameter("DB_USER"));
		properties.setProperty("DB_PWD", req.getParameter("DB_PWD"));
		properties.setProperty("VER_URL", req.getParameter("VER_URL"));
		properties.setProperty("VER_USER", req.getParameter("VER_USER"));
		properties.setProperty("VER_PWD", req.getParameter("VER_PWD"));
		properties.setProperty("VER_PRV", req.getParameter("VER_PRV"));
		properties.setProperty("VER_PPWD", req.getParameter("VER_PPWD"));
		properties.setProperty("DEFAULT_PRIORITY", req.getParameter("DEFAULT_PRIORITY"));
		properties.setProperty("DEFAULT_PERIOD", req.getParameter("DEFAULT_PERIOD"));
		properties.setProperty("DEFAULT_EMAIL(S)", req.getParameter("DEFAULT_EMAIL(S)"));
		File workspace = new File(System.getProperty("user.home") + File.separator + ".quality-one" + File.separator
				+ "workspace");
		if (workspace.isDirectory())
		{
			for (File project : workspace.listFiles())
			{
				if (!project.isDirectory())
				{
					continue;
				}
				if (!project.getName().equalsIgnoreCase(req.getParameter("KEE").replace(':', '$')))
				{
					continue;
				}

				for (File file : project.listFiles())
				{
					if (file.isFile() && file.getName().equalsIgnoreCase("config.properties"))
					{
						try
						{
							properties.store(new FileOutputStream(file), "");
						} catch (FileNotFoundException e)
						{
							e.printStackTrace();
						} catch (IOException e)
						{
							e.printStackTrace();
						}
					}
				}
			}
		}
		return true;
	}

	private boolean create(HttpServletRequest req)
	{
		Properties properties = new Properties();
		properties.setProperty("KEE", req.getParameter("KEE"));
		properties.setProperty("PORTAL_URL", req.getParameter("PORTAL_URL"));
		properties.setProperty("DB_DRIVER", req.getParameter("DB_DRIVER"));
		properties.setProperty("DB_URL", req.getParameter("DB_URL"));
		properties.setProperty("DB_USER", req.getParameter("DB_USER"));
		properties.setProperty("DB_PWD", req.getParameter("DB_PWD"));
		properties.setProperty("VER_URL", req.getParameter("VER_URL"));
		properties.setProperty("VER_USER", req.getParameter("VER_USER"));
		properties.setProperty("VER_PWD", req.getParameter("VER_PWD"));
		properties.setProperty("VER_PRV", req.getParameter("VER_PRV"));
		properties.setProperty("VER_PPWD", req.getParameter("VER_PPWD"));
		properties.setProperty("DEFAULT_PRIORITY", req.getParameter("DEFAULT_PRIORITY"));
		properties.setProperty("DEFAULT_PERIOD", req.getParameter("DEFAULT_PERIOD"));
		properties.setProperty("DEFAULT_EMAIL(S)", req.getParameter("DEFAULT_EMAIL(S)"));
		File workspace = new File(System.getProperty("user.home") + File.separator + ".quality-one" + File.separator
				+ "workspace");
		if (workspace.isDirectory())
		{
			File projectDir = new File(System.getProperty("user.home") + File.separator + ".quality-one"
					+ File.separator + "workspace" + File.separator + req.getParameter("KEE").replace(':', '$'));
			if (!projectDir.exists())
			{
				projectDir.mkdir();
			}
			File configFile = new File(System.getProperty("user.home") + File.separator + ".quality-one"
					+ File.separator + "workspace" + File.separator + req.getParameter("KEE").replace(':', '$')
					+ File.separator + "config.properties");
			if (!configFile.exists())
			{
				try
				{
					configFile.createNewFile();
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
			try
			{
				properties.store(new FileOutputStream(configFile), null);
			} catch (FileNotFoundException e)
			{
				e.printStackTrace();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		return true;
	}

	private boolean delete(HttpServletRequest req)
	{
		String kee = req.getParameter("KEE");
		File project = new File(System.getProperty("user.home") + File.separator + ".quality-one" + File.separator
				+ "workspace" + File.separator + req.getParameter("KEE").replace(':', '$'));
		if(project.exists())
		{
			if(project.isDirectory())
			{
				for(File file:project.listFiles())
				{
					file.delete();
				}
			}
			project.delete();
		}
		return true;
	}
}
